package se19team1e.gui.awt;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import se19team1e.controller.Application;
import se19team1e.controller.LoginController;
import se19team1e.exception.DataAccessException;
import se19team1e.gui.LoginForm;
import se19team1e.gui.MessageBoxHelper;

/**
 * 
 * A GUI form that allows the registered user to input login credentials. 
 * 
 * @author Li Guan Jun
 *
 */
@SuppressWarnings("serial")
public class AWTLoginForm extends AWTBaseScreen implements LoginForm {
    
    /**
     * A reference to the current owning controller of this form.
     */
    private LoginController controller;
    
    /**
     * The login input panel.
     */
    private final LoginInputPanel loginInputPanel;
    
    /**
     * An awt button to submit login credentials.
     */
    private final Button submitButton;

    /**
     * Constructs a login form, initializing with the application instance.
     * @param application Reference to the current application instance.
     */
    @SuppressWarnings("deprecation")
    public AWTLoginForm( Application application ) {
        super(application);
        this.setBounds(150, 50, 50, 20 );  
        // initialize layout
        this.setLayout( new BorderLayout() );
        
        // create panel to hold all sub panels
        Panel mainPanel = new Panel();
        mainPanel.setLayout( new GridLayout( 2, 0 ) );
        
        // create input panel
        this.loginInputPanel = new LoginInputPanel();
        
        // add panel to center panel
        mainPanel.add( this.loginInputPanel );
        
        // create panel to hold buttons
        Panel buttonPanel = new Panel();
        buttonPanel.setLayout( new GridLayout( 0, 1 ) );
        
        // create and add submit button
        this.submitButton = new Button( "Submit" );
        // add callback for submit button
        this.submitButton.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed( ActionEvent e ) {
                try {
					login();
				} catch (DataAccessException e1) {
					e1.printStackTrace();
				}
            }
        });
        buttonPanel.add( this.submitButton );

        // add panel to center panel
        mainPanel.add( buttonPanel );
        
        // add panel to frame
        this.add( mainPanel, BorderLayout.CENTER );
        
        // pack the layout
        this.pack();
        
        // hide the frame
        this.hide();
                
    }

    /* (non-Javadoc)
     * @see se19team1e.IBaseScreen#updateAvailableActions()
     */
    @Override
    public void updateAvailableActions() {
        this.updateAvailableMenuActions();
        
        // ensure submit button is enabled
        submitButton.setEnabled( true );
    }

    /* (non-Javadoc)
     * @see se19team1e.ILoginForm#initialize(se19team1e.LoginController)
     */
    @Override
    public void initialize( LoginController controller ) {
        this.controller = controller;
        
        // clears any previous input 
        this.loginInputPanel.initialize();
    }
    
    /**
     * Triggers a login of the current entered login credentials.
     * @throws DataAccessException 
     */
    public void login() throws DataAccessException {
        // validate first
        if ( !this.validateInput() ) {
            // failure, return
            return;
        }
        
        // delegate to controller
        this.controller.login( this.loginInputPanel.getUsernameInput(), this.loginInputPanel.getPasswordInput() );
    }

    /**
     * Validates the login credentials input to see if it is valid for login.
     * 
     * <p>
     * Delegates to the login input panel.
     * </p>
     * @return True if valid.
     */
    private boolean validateInput() {
        return this.loginInputPanel.validateInput( new MessageBoxHelper( this.getApplication() ) );
    }

}
